using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;
using Comteck.Dto.Sys;
using Comteck.Entities.Sys;
using Comteck.Ktt.Services.Infrastructure;
using ExpressMapper.Extensions;

namespace Comteck.Ktt.Services.Sys {
  public class ParameterService : BaseKttService, IParameterService {
    private readonly IKttRepository<Parameter, string> _ParameterRepository;

    public ParameterService(IKttUnitOfWork unitOfWork) : base(unitOfWork) {
      _ParameterRepository = _unitOfWork.GetRepository<Parameter, string>();
    }
    /// <summary>
    /// 查询全部列表
    /// </summary>
    /// <returns></returns>
    public async Task<IEnumerable<ParameterDto>> GetAllAsync() {
      var list = await _ParameterRepository.GetAllAsync();
      return list.MapTo<IEnumerable<ParameterDto>>();
    }

    /// <summary>
    /// 获取参数值
    /// </summary>
    /// <param name="enumParameter"></param>
    /// <returns></returns>
    public async Task<string> GetValueAsync(EnumParameter enumParameter) {
      var result = await _ParameterRepository.GetByIdAsync(enumParameter.ToString().ToLower());
      return result?.Value;
    }

    /// <summary>
    /// 批量更新
    /// </summary>
    /// <param name="list"></param>
    /// <returns></returns>
    public async Task SaveBatchAsync(IEnumerable<ParameterDto> list) {
      if (list.IsNullOrEmpty()) {
        return;
      }

      //var ids = list.Select(y => y.Id);
      //var dbList = await _ParameterRepository.TableUntracked.Where(x => ids.Contains(x.Id)).ToListAsync();
      //list.MapTo(dbList);

      var dbList = list.MapTo<List<Parameter>>();
      await _ParameterRepository.UpdateRangeAsync(dbList);
    }
  }
}
